<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();

$tabs=array(
	array('op'=>'list','title'=>'分红列表'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['ordersn']){
		$params['ordersn']=$_RQ['ordersn'];
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('commission',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['address']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
			if($item['remark']=='解冻分红' && $item['type']=='2'){
				$level_id=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'level_id');
				$this->update('commission',array('type'=>7,'level'=>$level_id),array('id'=>$item['id'],'type'=>2,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：为控制波比值，直推奖励是极差模式！同等级的直推是没有奖励的。','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'股东','type'=>'member'),
			array('name'=>'level','title'=>'股东等级','type'=>'select','options'=>'levels'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'money','title'=>'分红','type'=>'string'),
			array('name'=>'dong_fee','title'=>'冻结分红','type'=>'string'),
			array('name'=>'jifen','title'=>'积分(金币)','type'=>'string'),
			array('name'=>'type','title'=>'分红等级','type'=>'radio','options'=>'types'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'分红状态','type'=>'radio','options'=>'commission_status'),
			
			array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'detail','title'=>'分红详情'),
			array('type'=>'modal','op'=>'change','title'=>'修改佣金'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'ordersn','title'=>'订单号'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'分红状态','options'=>'commission_status'),
		),
	);
	$return['htmls']=$htmls;
	$return['types']=$this->fee_types;
	$return['levels']=$this->getall('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'预选代理');
	$return['commission_status']=$this->commission_status;
	exi($return);
}
if($_RQ['op']=='detail'){
	$item=$this->get('commission',array('id'=>intval($_RQ['id'])),array('id','remark'));
	if(!$item){
		exi('指定分红不存在！','error');
	}
	$item['remark']=$this->model('commission')->commission_remark($item['remark']);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'分红明细',
		'look'=>array(
			array('type'=>'text','name'=>'remark','title'=>'分红明细'),
		),
	);
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->update('commission',array('money'=>$post['money']),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('修改佣金成功！','','list');
	}
	$item=$this->get('commission',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('id','money'));
	if(!$item){
		exi('指定佣金不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改佣金',
		'edit'=>array(
			array('type'=>'string','name'=>'money','title'=>'设置佣金','help'=>'佣金异常时操作'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('commission',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('commission',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}